package com.hbk.codemaker;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JPanel;

import com.hbk.programe.Field;

public class FieldsPanel extends JPanel{
	
	public static final int MIN_HEIGHT=20;
	
	private int width=1024;
	
	private MainFrame window;
		
	public FieldsPanel(int width,MainFrame frame) {
		this.width=width;
		this.window=frame;
		setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
	}
	
	public void addField() {
		FieldPanel panel=new FieldPanel(this.width,window);
		add(panel);
		repaint();
	}
	
	public void setFields(List<Field> fields) {
		removeAll();
		for (Field field : fields) {
			FieldPanel panel=new FieldPanel(this.width,window);
			panel.setField(field);
			add(panel);
		}
		repaint(width);
	}
	
	public void repaint(int width) {
		this.width=width;
		int count=getComponentCount();
		setPreferredSize(new Dimension(width, (FieldPanel.FIELD_HEIGHT*count)+MIN_HEIGHT));
		repaint();
		
		for(int i=0;i<count;i++) {
			((FieldPanel)getComponent(i)).repaint(width);;
		}
	}

}
